home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14052 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
  2. From: grantp@usa.pipeline.com(Pete Grant)
  3. Newsgroups: comp.lang.c++,
  4. Subject: Re: Derivation and calling virtual functions
  5. Date: 28 Mar 1996 23:16:07 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4jf6jn$gni@news1.h1.usa.pipeline.com>
  8. References: <graphix.828032689@spiff.cc.iastate.edu>
  9. NNTP-Posting-Host: 38.8.120.15
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete Grant)
  13. X-Newsreader: Pipeline v3.5.0
  14.  
  15. On Mar 28, 1996 17:04:49 in article <Derivation and calling virtual
  16. functions>, 'graphix@iastate.edu (Kent A Vander Velden)' wrote: 
  17.  
  18.  
  19. >Say we have the following: 
  20. >class Base { 
  21. >public: 
  22. >virtual void func() { // some stuff } 
  23. >virtual void write() { func(); }; 
  24. >}; 
  25. >class Derived : public Base { 
  26. >public: 
  27. >void func() { // some stuff } 
  28. >write(); { Base::func(); } 
  29. >}; 
  30. >Derived inst; 
  31. >Now, when I call inst.write() it in turn calls Bass::write() which in 
  32. >turn calls Base::func().  Is there a way to instead have it call 
  33. >Derived::func() if the instance is of type Derived?  I hoped the  
  34. >virtual keyword would help in this case. 
  35.  
  36. No, inst.write() calls Base::func().  If you recoded your sample 
  37. so that Derived::write() called Base::write(), you'd find that 
  38. Derived::func() would wind up being called.  (Unless your 
  39. compiler is broken). 
  40.  
  41. -- 
  42. Pete Grant 
  43. Kalevi, Inc. 
  44. Software Engineering & development
  45.